package com.sgb.member.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.sgb.member.MemberConfig;
import com.sgb.member.R;
import com.sgb.member.databinding.ActivityLeverSettingEditBinding;
import com.sgb.member.entity.MemberLeverBean;
import com.sgb.member.entity.ParamDtosEntity;
import com.sgb.member.view_model.LeverSettingEditModel;
import com.sgb.member.weight.AppendFieldsView;
import com.sgb.member.weight.NumberInputFilter;
import com.sgb.member.weight.SimpleResult;
import com.swgk.core.base.AppActivity;
import com.swgk.core.customview.ItemInputTextView;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.ClickUtils;
import com.swgk.core.util.MToast;
import com.swgk.core.util.StringUtil;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.ArrayList;
import java.util.List;

public class LeverSettingEditActivity extends AppActivity {

    private LeverSettingEditModel sModel;
    private ActivityLeverSettingEditBinding mBinding;

    private MemberLeverBean mLeverBean;

    public static void start(Activity context, MemberLeverBean leverBean) {
        Intent intent = new Intent(context, LeverSettingEditActivity.class);
        intent.putExtra(MemberConfig.EXTRA_ENTITY, leverBean);
        context.startActivityForResult(intent, MemberConfig.REQ_SERVICE);
    }

    @Override
    protected void initView() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_lever_setting_edit);
        sModel = new ViewModelProvider(this).get(LeverSettingEditModel.class);
        mLeverBean = (MemberLeverBean) getIntent().getSerializableExtra(MemberConfig.EXTRA_ENTITY);
        mBinding.activityBar.titleView.setText("编辑等级评定标准");
        mBinding.activityBar.ivBack.setOnClickListener(v -> onBackPressed());

        // 1~50人、50~100人、100~500人、500~1000人、1000~2000人、2000人以上
        mBinding.itemRequirements.setOnWholeItemClickListener(new ItemInputTextView.OnWholeItemClickListener() {
            @Override
            public void OnWholeItemClick(View view) {
                sModel.showPersonnelRequirementsPop(mBinding.itemRequirements, new SimpleResult<CommonPopEntity>() {
                    @Override
                    public void onResult(CommonPopEntity entity, Object... objects) {
                    }
                });
            }
        });
        mBinding.submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!ClickUtils.isDoubleClick2()) {
                    submit();
                }
            }
        });
        sModel.dialog.observe(this, aBoolean -> {
            if (aBoolean) {
                if (!DialogHelper.isShowing()) {
                    DialogHelper.showProgressDialog(this, "数据获取中...");
                }
            } else {
                DialogHelper.dismissProgressDialog();
            }
        });
        sModel.mSubmit.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                if (aBoolean) {
                    Intent intent = new Intent();
                    intent.putExtra(MemberConfig.EXTRA_ENTITY, mLeverBean);
                    setResult(RESULT_OK, intent);
                    finish();
                }
            }
        });
        sModel.mLeverDetail.observe(this, new Observer<MemberLeverBean>() {
            @Override
            public void onChanged(MemberLeverBean leverBean) {
                if (leverBean != null) {
                    mLeverBean = leverBean;
                }
                initLeverInfo();
            }
        });
        mBinding.itemRegisterMoney.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        mBinding.itemRegisterMoney.setFilters(new NumberInputFilter[]{new NumberInputFilter(999999999.99, 0.01, 2)});
        mBinding.itemProfit.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        mBinding.itemProfit.setFilters(new NumberInputFilter[]{new NumberInputFilter(999999999.99, 0.01, 2)});
        mBinding.itemMemberRequirements.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        mBinding.itemMemberRequirements.setFilters(new NumberInputFilter[]{new NumberInputFilter(999999999.99, 0.01, 2)});
        mBinding.itemYearMoney.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        mBinding.itemYearMoney.setFilters(new NumberInputFilter[]{new NumberInputFilter(999999999.99, 0.01, 2)});

        sModel.getLeverDetail(mLeverBean);
    }

    private void initLeverInfo() {
        mBinding.itemLever.setContent(mLeverBean.getLevel());
        mBinding.itemRegisterMoney.setContent(mLeverBean.getRegisteredCapital());
        mBinding.itemProfit.setContent(mLeverBean.getYearProfit());
        mBinding.itemRequirements.setContent(mLeverBean.getResearchersAsked());
        mBinding.itemMemberRequirements.setContent(mLeverBean.getMembershipRequirements());
        mBinding.itemYearMoney.setContent(mLeverBean.getAnnualTurnover());
        mBinding.etProfessionalQualification.setContent(mLeverBean.getProfessionalQualifications());
        mBinding.etSpecialAssets.setContent(mLeverBean.getStandard());
        mBinding.etPerformance.setContent(mLeverBean.getPerformanceRequirements());
        mBinding.etQualification.setContent(mLeverBean.getQualificationRequirements());
        List<ParamDtosEntity> paramDtos = mLeverBean.getParamDtos();
        if (paramDtos != null && paramDtos.size() > 0) {
            List<AppendFieldsView.AppendField> appendFields = new ArrayList<>();
            for (ParamDtosEntity paramDto : paramDtos) {
                appendFields.add(new AppendFieldsView.AppendField(paramDto.title, paramDto.content));
            }
            mBinding.fieldsLayout.setData(appendFields);
        }
    }

    private void submit() {
        String mRegisterMoney = mBinding.itemRegisterMoney.getContent(); // 注册资金
//        if (StringUtil.isEmpty(mRegisterMoney)) {
//            MToast.showToast(LeverSettingEditActivity.this, "请输入注册资金");
//            return;
//        }
        String mYearProfit = mBinding.itemProfit.getContent(); // 年利润
//        if (StringUtil.isEmpty(mYearProfit)) {
//            MToast.showToast(LeverSettingEditActivity.this, "请输入年利润");
//            return;
//        }
        String mPersonRequirements = mBinding.itemRequirements.getContent(); // 人员要求
        if (StringUtil.isEmpty(mPersonRequirements)) {
            MToast.showToast(LeverSettingEditActivity.this, "请选择人员要求");
            return;
        }
        String mMemberRequirements = mBinding.itemMemberRequirements.getContent(); // 会费要求
        if (StringUtil.isEmpty(mMemberRequirements)) {
            MToast.showToast(LeverSettingEditActivity.this, "请输入会费要求");
            return;
        }
        String mYearMoney = mBinding.itemYearMoney.getContent(); // 年营业额
        if (StringUtil.isEmpty(mYearMoney)) {
            MToast.showToast(LeverSettingEditActivity.this, "请输入年营业额");
            return;
        }
        String mProfessionalQualification = mBinding.etProfessionalQualification.getContent(); // 专业资质
        if (StringUtil.isEmpty(mProfessionalQualification)) {
            MToast.showToast(LeverSettingEditActivity.this, "请填写300字以内专业资质");
            return;
        }
        String mSpecialAssets = mBinding.etSpecialAssets.getContent(); // 专项资产
//        if (StringUtil.isEmpty(mSpecialAssets)) {
//            MToast.showToast(LeverSettingEditActivity.this, "请填写300字以内专项资产");
//            return;
//        }
        String mPerformance = mBinding.etPerformance.getContent(); // 业绩要求
        if (StringUtil.isEmpty(mPerformance)) {
            MToast.showToast(LeverSettingEditActivity.this, "请填写300字以内业绩要求");
            return;
        }
        String mQualification = mBinding.etQualification.getContent(); // 资质要求
        if (StringUtil.isEmpty(mQualification)) {
            MToast.showToast(LeverSettingEditActivity.this, "请填写300字以内资质要求");
            return;
        }
        mLeverBean.setRegisteredCapital(mRegisterMoney);
        mLeverBean.setYearProfit(mYearProfit);
        mLeverBean.setResearchersAsked(mPersonRequirements);
        mLeverBean.setMembershipRequirements(mMemberRequirements);
        mLeverBean.setAnnualTurnover(mYearMoney);
        mLeverBean.setProfessionalQualifications(mProfessionalQualification);
        mLeverBean.setStandard(mSpecialAssets);
        mLeverBean.setPerformanceRequirements(mPerformance);
        mLeverBean.setQualificationRequirements(mQualification);
        // 自定义参数
        List<AppendFieldsView.AppendField> data = mBinding.fieldsLayout.getData();
        List<ParamDtosEntity> paramDtos = new ArrayList<>();
        if (data != null && data.size() > 0) {
            for (AppendFieldsView.AppendField datum : data) {
                if (!TextUtils.isEmpty(datum.title) && !TextUtils.isEmpty(datum.content)) {
                    paramDtos.add(new ParamDtosEntity(datum.title, datum.content));
                }
            }
        }
        mLeverBean.setParamDtos(paramDtos);
        sModel.submitLever(mLeverBean);
    }
}
